<?php

namespace App\Console\Commands;

use App\Model\User;
use Illuminate\Console\Command;
use DB;

class Migrate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:once';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = DB::connection('old_mysql')->select('select * from user WHERE passwd is not null');
        foreach ($users as $user) {
            $newUser = User::firstOrNew(['number' => $user->stuNum]);
            $newUser->open_id = $user->openid;
            $newUser->number = $user->stuNum;
            $newUser->gender = $user->sex == '男' ? 1 : 0;
            $newUser->password = $user->passwd;
            $newUser->role = 0;
            $newUser->name = $user->name;
            $newUser->college = $user->college;
            $newUser->class = $user->class;
            $newUser->major = $user->major;
            $newUser->home_address = $user->home;
            $newUser->area = $user->hometown ? : $user->area;
            $newUser->high_school = $user->highschool;
            $newUser->cellphone = $user->telnumber;
            $newUser->short_phone = $user->shortnumber;
            $newUser->birthday = $user->birthday;
            $newUser->identity_number = $user->identify;
            $newUser->save();
        }
    }
}
